package middleSearch;

/**
 * @description: 描述
 * @author: Mr.Hu
 * @create: 2022/3/18 10:34
 **/
public class MiddleSearch {
    public static void main(String[] args) {
       int[] nums = {-1,0,3,5,9,12};
       int target =2;
        MiddleSearch middleSearch = new MiddleSearch();
        int result = middleSearch.search(nums, target);
        System.out.println(result);

    }
    public static int search(int[] nums, int target) {
        int tail = nums.length-1;
        int head = 0;
        int middle ;
        while (head<=tail){
            middle = (tail - head) / 2 + head;
            if (nums[middle]>target){
                tail = middle-1;
            }else if (nums[middle]<target){
                head = middle+1;
            }else if (nums[middle]== target){
                return middle;
            }
        }
        return -1;
    }
}
